package com.sopaths.crawler.task.handler;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;

import com.sopaths.contract.crawler.CrawlerTaskType;
import com.sopaths.contract.crawler.RecruitmentSummary;
import com.sopaths.crawler.tasks.CrawlerTask;
import com.sopaths.crawler.tasks.RecruitmentCrawlerTask;
import com.sopaths.crawler.tasks.SummaryCrawlerTask;
import com.sopaths.crawler.thread.CrawlerTaskExecutor;

@Named
public class SummaryCrawlerTaskResultHandler implements CrawlerTaskResultHandler {

	private Logger logger = Logger.getLogger(SummaryCrawlerTaskResultHandler.class);

	@Inject
	@Named("recruitmentCrawlerTaskExecutor")
	private CrawlerTaskExecutor taskExecutor;

	@Override
	public void handle(CrawlerTask<?> task) {
		try {
			SummaryCrawlerTask innerTask = (SummaryCrawlerTask) task;

			List<RecruitmentSummary> summaries = innerTask.getResult();
			for (RecruitmentSummary summary : summaries) {
				RecruitmentCrawlerTask recruitmentTask = new RecruitmentCrawlerTask();
				recruitmentTask.setSource(innerTask.getSource());
				recruitmentTask.setType(CrawlerTaskType.RecruitmentCrawlerTask);
				recruitmentTask.setUrl(summary.getUrl());
//				CrawlerTaskExecutor.submit(recruitmentTask);
				taskExecutor.submit(recruitmentTask);
			}
		} catch (Exception ex) {
			logger.error("An error occurred while handling task result.", ex);
		}
	}

	public void setTaskExecutor(CrawlerTaskExecutor taskExecutor) {
		this.taskExecutor = taskExecutor;
	}

}
